#ifndef PROTOCOLEDITOR_H
#define PROTOCOLEDITOR_H

#include "headerinclude.h"

namespace Ui
{
class ProtocolEditor;
}

class SystemProtocol;
class MsgPanelEditor;
class ProtocolEditor : public QMainWindow
{
    Q_OBJECT

public:
     typedef void(*FindDataProgressFunc)(float);

    ~ProtocolEditor();
    explicit ProtocolEditor(QWidget *parent = 0);

    VOID InitSysProtocolPanel();

    BOOL LoadSystemConfig(const QString& sPath,const QString& sFileName);                   //父协议数据加载
    BOOL LoadSubSysttemConfig(const QString& sPath, const INT nTypeId);          //子协议数据加载

public:

    VOID InIt();                                                    //启动初始化(默认选中第一父协议，子协议显示第一个父协议下的子协议信息没有显示空白)

    BOOL AddSystemItem(const UCHAR nTypeId,const QString sName);                            //创建父协议
    BOOL AddSubItem( const UCHAR nSubId, const QString sName, const ProtocolType nMsgType);                                  //创建子协议
    BOOL AddSubMsgDataItem(const PROTOCOLDATATYPE nTypeId, const QString sInfo,const QString  sDescribe);                  //创建数据

    VOID CreateDelMessageBox(const QString sTip,const PanelType eType);

    BOOL DelSystemItem();                             //删除父协议的时候默认选中下一个协议。如果没有选中上一个
    BOOL DelSubItem();                                   //同上
    BOOL DelSubMsgDataItem();

    BOOL SaveProtocolData(const QString& sPath);
    BOOL SaveSubProtocolData(const QString sFileName,const QString sPath);

    BOOL UpdateProtocolFile(const INT nType,const INT nId,const QString sData);
    BOOL UpdateProtocolFile(const INT nId);

    BOOL UpdateSubFile(const INT nId,const,QString sData);
    BOOL UpdateSubFile(const INT nId,const);

    VOID UpdateItemInfo(const INT nTypeId=0, INT nSubId = 0);                                                                        //更新数据(点击父协议的时候load子协议数据更新显示到子协议panel)
    VOID UpdateSubInfo(const INT nTypeId=0, INT nSubId = 0,ProtocolType type =SERVER);
    VOID UpdateSubDataInfo(const INT nTypeId=0, INT nSubId = 0,ProtocolType type =SERVER, INT nSubInfoId = 0 );

    VOID FindItem(const QString sMsg);                                                                                                                              //查找信息

    /*SVN操作*/
    VOID CommitSVN(const QString sFilePath);
    VOID UpdateSVN(const QString sFilePath);


public:
    PSYSPRODEF  GetSysDataById(const INT nSysId);
    PSUBPRODEF GetSubSysData(const INT nSysId,const INT nSubId,const INT nMsgType);
    PSUBPRODATAINFO GetSubDataInfoById(const INT nSysId, const INT nSubId, const INT nMsgType,const INT nMsgTypeId);

public:
    VOID InitToolBarEvent();


    inline VOID UpdateCurSelect(const INT nSysId = 0,const INT nSubId = 1,const INT nSubDataId = 0)
    {
        m_CurSelSysIdx = nSysId;m_CurSelSubIdx = nSubId;m_CurSelSubInfoIdx = nSubDataId;
    }
    inline QString GetDataInfoType(const PROTOCOLDATATYPE nTypeId)
    {
        switch (nTypeId)
        {
        case T_BOOL:
            return "bool";
        case T_CHAR:
            return "char";
        case T_UCHAR:
            return "unsinged char";
        case T_SHORT:
            return "short";
        case T_USHORT:
            return "unsinged chort";
        case T_INT:
            return "int";
        case T_UINT:
            return "unsinged int";
        case T_INT64:
            return "int64";
        case T_UINT64:
            return "unsinged int64";
        case T_FLOAT:
            return "float";
        case T_DOUBLE:
            return "double";
        case T_STRUCT:
            return "struct";
        case T_UNION:
            return "union";
        case T_STRING:
            return "string";
        case T_ARRAY:
            return "array";
        default:
            return "";
        }
    }
    inline INT GetSysId()
    {
        auto items = ui->ProtocolSys->selectedItems();
        if(items.isEmpty()) return -1;
        return items.at(0)->text().toInt();
    }

    inline INT GetSubId()
    {
        auto items = ui->ProtocolSub->selectedItems();
        if(items.isEmpty()) return -1;
        return items.at(0)->text().toInt();
    }



private slots:
     void on_ProtocolSys_clicked(const QModelIndex &index);

     void on_ProtocolSys_doubleClicked(const QModelIndex &index);

     void on_ProtocolSub_clicked(const QModelIndex &index);

     void on_ProtocolSub_doubleClicked(const QModelIndex &index);

     void on_Msgexplain_doubleClicked(const QModelIndex &index);

     void on_Msgexplain_clicked(const QModelIndex &index);
     void SortTableByCol(int nCol);

     void FindSysData(QString,INT);
     void SysEditBtnSlot(INT,QString,EditType);
     void SubEditBtnSlot(INT,QString,INT,EditType);
     void SubDataEditBtnSlot(INT,QString,QString,EditType);
private:
    INT                     m_CurSelSysIdx;     //当前选中的父协议id
    INT                     m_CurSelSubIdx;     //当前选中的子协议id
    INT                     m_CurSelSubInfoIdx; //当前选中的协议信息id
    QList<QTableWidgetItem*>    m_CurSelItems;
    ProtocolType            m_SubProtocolType;
    Ui::ProtocolEditor*     ui;

    QVector<SYSPRODEF>     m_SysProData;
    MsgPanelEditor*         m_MsgEditor;
};



#endif // PROTOCOLEDITOR_H
